<?php
namespace Seller\Controller;
use Think\Controller;
class ShopController extends CommonController {

    private $shop;

    public function _initialize() {
        $this->shop = D('Shop');
    }

    //店铺列表
    public function index() {
        //搜索条件
        $map = array();
        $btn = I('get.btn');
        if ( !empty($btn) ) {
            $state = I('get.state');
            if ($state != -2) $map['state'] = $state;
            $shop_name = trim(I('get.shop_name'));
            if ( !empty($shop_name) ) $map['shop_name'] = array('like',"%$shop_name%");
        }
        //分页和查询数据
        $total = $this->shop->where($map)->count();
        $page = set_page($total, 2);
        $shop = $this->shop->where($map)->limit($page->firstRow.','.$page->listRows)->order('id DESC')->select();
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('shop', $shop);
        $this->assign('shop_name', $shop_name);
        $this->assign('state', $state);
        $this->display();
    }


    //已通过店铺
    public function pass() {
        $this->showList(1);
    }

    //不通过店铺
    public function noPass() {
        $this->showList(-1);
    }

    //待审核店铺
    public function check() {
        $this->showList(0);
    }

    //可以传递不同的状态的店铺列表
    private function showList($state) {
        //搜索条件
        $map = array();
        $btn = I('get.btn');
        $map['state'] = $state;
        if ( !empty($btn) ) {
            $shop_name = trim(I('get.shop_name'));
            if ( !empty($shop_name) ) $map['shop_name'] = array('like',"%$shop_name%");
        }
        //分页和查询数据
        $total = $this->shop->where($map)->count();
        $page = set_page($total, 2);
        $shop = $this->shop->where($map)->limit($page->firstRow.','.$page->listRows)->order('id DESC')->select();
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('shop', $shop);
        $this->assign('shop_name', $shop_name);
        $this->display();
    }


    //添加店铺
    public function createShop() {
        if (IS_POST) {
            if ($this->shop->create()) {
                $data = array(
                    'shop_name' => I('post.shop_name'),
                    'company' => I('post.company'),
                    'keeper' => I('post.keeper'),
                    'phone' => I('post.phone'),
                    'password' => sha1(trim(I('post.password'))),
                    'tel' => I('post.tel'),
                    'address' => I('post.address'),
                    'bank_no' => I('post.bank_no'),
                    'bank_user_name' => I('post.bank_user_name'),
                    'create_desc' => I('post.create_desc'),
                    'state' => I('post.state'),
                    'license_img' => I('post.license_img'),
                    'create_time' => time()
                );
                $bool = $this->shop->add($data);
                $bool ? $this->success('新增成功', U('Shop/index')) : $this->error('提交失败!');
            } else {
                $error_info = $this->shop->getError();
                $this->error($error_info);
            }
        } else {
            $this->display();
        }
    }

    //店铺信息
    public function getShop() {
        if (IS_POST) {
            $id = I('post.id');
            $shop = $this->shop->where( array('id'=>$id) )->find();
            //如果填写密码则修改，为空就还是原来的密码
            $new_password = trim(I('post.password'));
            $password = empty($new_password) ? $shop['password'] : sha1($new_password);
            $data = array(
                'shop_name' => I('post.shop_name'),
                'company' => I('post.company'),
                'keeper' => I('post.keeper'),
                'phone' => I('post.phone'),
                'password' => $password,
                'tel' => I('post.tel'),
                'address' => I('post.address'),
                'bank_no' => I('post.bank_no'),
                'bank_user_name' => I('post.bank_user_name'),
                'create_desc' => I('post.create_desc'),
                'state' => I('post.state'),
                'license_img' => I('post.license_img'),
                'create_time' => time()
            );

            if ($this->shop->create()) {
                unlink($shop['license_img']);
                $bool = $this->shop->where( array('id'=>$id) )->save($data);
                $bool ? $this->success('修改成功', U('Shop/index')) : $this->error('修改失败!');
            } else {
                $error_info = $this->shop->getError();
                $this->error($error_info);
            }

        } else {
            $id = I('get.id');
            $shop = $this->shop->where( array('id'=>$id) )->find();
            $this->assign('shop',$shop);
            $this->assign('id',$id);
            $this->display();
        }
    }


}